home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / FtpClient.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  7.7 KB  |  412 lines

  1. /*
  2.  *
  3. */
  4.  
  5. package symantec.itools.db.awt;
  6.  
  7. import java.net.*;
  8. import java.io.*;
  9. import java.awt.*;
  10. import java.util.*;
  11.  
  12. /**
  13.  * This class implements a subset of the FTP client-side protocol.
  14.  * It allows you to connect, login and retrieve files
  15.  */
  16.  
  17. public class FtpClient
  18. {
  19.     String host;
  20.     Socket s;
  21.     boolean Connected = false;
  22.     boolean LoggedIn = false;
  23.     PrintStream ftpout;
  24.     DataInputStream ftpin;
  25.     FtpErrors errs;
  26.     Vector v;
  27.     Vector files;
  28.     Vector dirs;
  29.     private boolean dataRead = false;
  30.     public static final int ASCII = 0;
  31.     public static final int BINARY = 1;
  32.     private int xferMode = BINARY;
  33.     private boolean modeChange = true;
  34.  
  35.     /** This is the main constructor
  36.      */
  37.  
  38.     public FtpClient(String hostname) throws IOException
  39.     {
  40.         byte buf[] = new byte[3];
  41.         String str;
  42.         int ret;
  43.  
  44.         this.host = hostname;
  45.         Connected = true;
  46.  
  47.         errs = new FtpErrors();
  48.  
  49.         try {
  50.             s = new Socket(hostname, 21);
  51.         } catch (UnknownHostException e) {
  52.             Connected = false;
  53.         }
  54.  
  55.         ftpout = new PrintStream(s.getOutputStream());
  56.         ftpin = new DataInputStream(s.getInputStream());
  57.  
  58.         str = ftpin.readLine();
  59.  
  60.         try {
  61.             ret = Integer.parseInt(str.substring(0, 3));
  62.         } catch (NumberFormatException e) {
  63.             ret = -1;
  64.         } catch (StringIndexOutOfBoundsException e) {
  65.             ret = -1;
  66.         }
  67.  
  68.         if (ret == 220) {
  69.             //read the server info
  70.             str = ftpin.readLine();
  71.         } else {
  72.             throw new IOException("Connect");
  73.         }
  74.     }
  75.  
  76.     /** Set transfer type here
  77.      */
  78.  
  79.     public void setXferMode(int mode)
  80.     {
  81.         if (mode != xferMode && (mode == ASCII || mode == BINARY)) {
  82.             xferMode = mode;
  83.             modeChange = true;
  84.         }
  85.  
  86.         return;
  87.     }
  88.  
  89.     public int getXferMode()
  90.     {
  91.             return xferMode;
  92.     }
  93.  
  94.     /** Login in with user name and passwd
  95.         returns true/false
  96.      */
  97.     public void login(String user, String passwd)
  98.         throws FtpLoginException
  99.     {
  100.         int retCode;
  101.  
  102.         if (user == null) {
  103.             throw new FtpLoginException("Login cannot be null");
  104.         }
  105.  
  106.         try {
  107.             if ((retCode = doCmd("USER " + user + "\n")) != 331) {
  108.                 throw new FtpLoginException(getError(retCode));
  109.             }
  110.  
  111.             if ((retCode = doCmd("PASS " + passwd + "\n")) != 230) {
  112.                 throw new FtpLoginException(getError(retCode));
  113.             }
  114.  
  115.         } catch (IOException e) {
  116.             throw new FtpLoginException(e.getMessage());
  117.         }
  118.  
  119.         LoggedIn = true;
  120.     }
  121.  
  122.     public boolean loggedIn()
  123.     {
  124.         return LoggedIn;
  125.     }
  126.  
  127.     /** Implements the RETR command (get file)
  128.      */
  129.  
  130.     public DataInputStream getFile(String name)
  131.         throws IOException
  132.     {
  133.         Socket fileStream;
  134.         int rval;
  135.         String buf;
  136.  
  137.         if (modeChange) {
  138.             if (xferMode == ASCII)
  139.                 rval = doCmd("TYPE A\n");
  140.             else
  141.                 rval = doCmd("TYPE I\n");
  142.  
  143.             if (rval != 200) {
  144.                 throw new IOException("Couldn't change xfer mode");
  145.             }
  146.  
  147.             modeChange = false;
  148.         }
  149.  
  150.         fileStream = doIOCmd("RETR " + name + "\n");
  151.  
  152.         buf = ftpin.readLine();
  153.  
  154.         return (new DataInputStream(fileStream.getInputStream()));
  155.     }
  156.  
  157.     /** getFileList retrieves the list of files in the current directory
  158.      */
  159.  
  160.     public Vector getFileList()
  161.         throws IOException
  162.     {
  163.         int retval;
  164.         Socket s;
  165.         DataInputStream dis;
  166.         String buf;
  167.         String retStr[];
  168.         boolean done = false;
  169.         String tmp;
  170.  
  171.         if (!dataRead) {
  172.             v = new Vector(100);
  173.             files = new Vector(100);
  174.             dirs = new Vector(100);
  175.  
  176.             s = doIOCmd("LIST -aF\n");
  177.             dis = new DataInputStream(s.getInputStream());
  178.  
  179.             done = false;
  180.  
  181.             if (s == null)
  182.                 throw new IOException("Socket creation failed");
  183.             while (!done) {
  184.                 buf = dis.readLine();
  185.                 if (buf == null) {
  186.                     done = true;
  187.                 } else {
  188.                     v.addElement(buf);
  189.                 }
  190.             }
  191.  
  192.             buf = ftpin.readLine();
  193.  
  194.             s.close();
  195.         }
  196.  
  197.         for (int i = 1; i < v.size(); i++) {
  198.             tmp = (String)(v.elementAt(i));
  199.             if (tmp.charAt(tmp.length() - 1) == '/')
  200.                 dirs.addElement(getDirName((String)(v.elementAt(i))));
  201.             else
  202.                 files.addElement(getFileName((String)(v.elementAt(i))));
  203.         }
  204.  
  205.         dataRead = true;
  206.  
  207.         return files;
  208.     }
  209.  
  210.     public Vector getDirList()
  211.         throws IOException
  212.     {
  213.         Vector v;
  214.  
  215.         if (!dataRead)
  216.             v = getFileList();
  217.  
  218.         return dirs;
  219.     }
  220.  
  221.     /** Perform an IO Command which requires opening a passive socket
  222.      */
  223.  
  224.     private Socket doIOCmd(String cmd)
  225.         throws IOException
  226.     {
  227.         Socket s;
  228.         ServerSocket serv;
  229.            InetAddress hostAddr = InetAddress.getLocalHost();
  230.            byte addr[] = hostAddr.getAddress();
  231.         String newCmd = "PORT ";
  232.         int ret;
  233.  
  234.         serv = new ServerSocket(0);
  235.  
  236.         for (int i = 0; i < addr.length; i++) {
  237.             newCmd = newCmd + (addr[i] & 0xFF) + ",";
  238.         }
  239.  
  240.         newCmd = newCmd + ((serv.getLocalPort() >>> 8) & 0xff) + ","
  241.             + (serv.getLocalPort() & 0xff);
  242.  
  243.         ret = doCmd(newCmd + "\n");
  244.  
  245.         if ((ret != 200) && (ret != 226) && (ret !=230))
  246.             throw new IOException(getError(ret));
  247.  
  248.         ret = doCmd(cmd);
  249.  
  250.         if ((ret != 125) && (ret != 150) && (ret !=200))
  251.             throw new IOException(getError(ret));
  252.  
  253.         s = serv.accept();
  254.         serv.close();
  255.  
  256.         return s;
  257.     }
  258.  
  259.     public void chdir(String dir)
  260.         throws IOException
  261.     {
  262.         int retCode;
  263.  
  264.         dataRead = false;
  265.  
  266.         retCode = doCmd("CWD " + dir + "\n");
  267.  
  268.         if (retCode != 250 && retCode !=200) {
  269.             debug("Return code is: " + retCode);
  270.             throw new IOException("Couldn't cd to: " + dir);
  271.         }
  272.     }
  273.  
  274.     /** Issue a specific ftp command
  275.      */
  276.  
  277.     private int doCmd(String cmd)
  278.         throws IOException
  279.     {
  280.         byte buf[] = new byte[3];
  281.         int ret;
  282.         String str;
  283.  
  284.         ftpout.print(cmd);
  285.         ftpin.read(buf);
  286.         ftpin.readLine();
  287.  
  288.         str = new String(buf, 0);
  289.  
  290.         try {
  291.             ret = Integer.parseInt(str.substring(0, 3));
  292.         } catch (NumberFormatException e) {
  293.             ret = -1;
  294.         } catch (StringIndexOutOfBoundsException e) {
  295.             ret = -1;
  296.         }
  297.         return ret;
  298.     }
  299.  
  300.     public String getDirName(String s)
  301.     {
  302.         String tmp =null;
  303.         String ret;
  304.         StringTokenizer st1;
  305.  
  306.         st1 = new StringTokenizer(s);
  307.  
  308.         while (st1.hasMoreElements())
  309.         {
  310.             tmp = st1.nextToken();
  311.         }
  312.  
  313.         ret = tmp.substring(0, tmp.length() - 1);
  314.  
  315.         return ret;
  316.     }
  317.  
  318.     public String getFileName(String s)
  319.     {
  320.         String tmp =null;
  321.         String ret;
  322.         StringTokenizer st1;
  323.         char ch;
  324.  
  325.         st1 = new StringTokenizer(s);
  326.  
  327.         while (st1.hasMoreElements())
  328.         {
  329.             tmp = st1.nextToken();
  330.         }
  331.  
  332.         ch = tmp.charAt(tmp.length() - 1);
  333.  
  334.         if (ch == '*' || ch == '@' || ch == '=')
  335.             return tmp.substring(0, tmp.length() - 1);
  336.         else
  337.             return tmp;
  338.     }
  339.  
  340.  
  341.     public String getError(int err)
  342.     {
  343.         return errs.getError(err);
  344.     }
  345.  
  346.     public void closeHard()
  347.     {
  348.         try {
  349.             ftpin.close();
  350.             ftpout.close();
  351.         } catch (IOException e) {
  352.             // Ignore
  353.         }
  354.         LoggedIn = false;
  355.         dataRead = false;
  356.     }
  357.  
  358.     /** Close and quit
  359.      */
  360.     public void quit()
  361.         throws IOException
  362.     {
  363.         doCmd("QUIT\n");
  364.  
  365.         ftpin.close();
  366.         ftpout.close();
  367.     }
  368.  
  369.     /** Debugging routine
  370.      */
  371.  
  372.     private void debug(String s)
  373.     {
  374.         //System.err.println(s);
  375.     }
  376. }
  377.  
  378. class FtpErrors
  379. {
  380.     Hashtable errTable;
  381.  
  382.     public static final int errs[] =
  383.         { 500, 501, 502, 503, 504, 530, 532, 550, 551, 552, 553 };
  384.  
  385.     public static final String errstr[] = {
  386.             "Syntax error, command unrecognized",
  387.             "Syntax error in parameters or arguments",
  388.             "Command not implemented",
  389.             "Bad sequence of commands",
  390.             "Command not implemented for that parameter",
  391.             "Not logged in",
  392.             "Need account for storing files",
  393.             "Requested action not taken",
  394.             "Requested action aborted: page type unknown",
  395.             "Requested file action aborted",
  396.             "Requested action not taken"
  397.     };
  398.  
  399.     public FtpErrors()
  400.     {
  401.         errTable = new Hashtable();
  402.  
  403.         for (int i = 0; i < errs.length; i++)
  404.             errTable.put(new Integer(errs[i]), errstr[i]);
  405.     }
  406.  
  407.     public String getError(int errorkey)
  408.     {
  409.         return (String)(errTable.get(new Integer(errorkey)));
  410.     }
  411. }
  412.